package com.example.springbootshiro.controller;

import com.example.springbootshiro.response.CommonReturnData;
import com.example.springbootshiro.service.PermissionService;
import com.example.springbootshiro.shiro.config.MyCacheManage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by YangGang on 2021/12/15 14:08。
 */
@Slf4j
@RestController
@RequestMapping("/shiro/cache")
public class ShiroCacheController {
    @Autowired
    PermissionService permissionService;

    @GetMapping("/get")
    public Object get() {
        MyCacheManage.getAuthenticationCache();
        MyCacheManage.getAuthorizationCache();
        return CommonReturnData.success();
    }

    @PostMapping("/clear")
    public Object clear() {
        MyCacheManage.clearAuthenticationCache();
        MyCacheManage.clearAuthorizationCache();
        return CommonReturnData.success();
    }


    @PostMapping("/remove")
    public Object remove() {
        Subject subject = SecurityUtils.getSubject();
        String principal = (String) subject.getPrincipal();
        MyCacheManage.removeAuthCacheByKey(principal);
        return CommonReturnData.success();
    }


    @GetMapping("/test")
    public Object test() {
        return CommonReturnData.success(permissionService.findPermissionMapCache());
    }

}
